package com.jl.core.service.impl;

import com.jl.core.model.resp.ReConnectResp;
import com.jl.core.netty.MyWebSocketHandler;
import com.jl.core.service.CoalitionService;
import com.jl.core.service.GameService;
import com.jl.enums.GameType;
import com.jl.model.CoalitionRoomList;
import com.jl.model.vo.UserVo;
import com.jl.utils.UserInfoContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
public class GameServiceImpl implements GameService {

    private static final Integer coalitionId = 88888;

    @Autowired
    private CoalitionService coalitionService;

    @Override
    public ReConnectResp reConnect() {
        UserVo user = UserInfoContext.getUser();
        ReConnectResp gamingUser = MyWebSocketHandler.getGamingUser(user.getId());
        if (gamingUser!=null){
            if (GameType.type1.getId().equals(gamingUser.getGameType())){
                List<CoalitionRoomList> roomList = coalitionService.getRoomList(coalitionId, GameType.type1.getId(), gamingUser.getRoomId());
                gamingUser.setCoalitionRoomList(roomList.get(0));
            }else if ((GameType.type2.getId().equals(gamingUser.getGameType()))){
                List<CoalitionRoomList> roomList = coalitionService.getRoomList(coalitionId, GameType.type2.getId(), gamingUser.getRoomId());
                gamingUser.setCoalitionRoomList(roomList.get(0));
            }
            return gamingUser;
        }
        return null;
    }
}
